﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System.Linq;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.Utils;

namespace DevEx
{
    public partial class ucKho : DevExpress.XtraEditors.XtraUserControl
    {
        public ucKho()
        {
            InitializeComponent();
        }

        private void ucCCL_Load(object sender, EventArgs e)
        {
            KhoiTaodtgKho("");
            KhoiTaoGiaoDien(true);
        }

        public void KhoiTaodtgKho(String Sname)
        {
            //CKho kho = new CKho();
            //dtgKho.DataSource = kho.SelectAllRecord();

            QLVTDataContext dc = new QLVTDataContext();
            var q = from p in dc.tblKhos select new { p.ID_Kho , p.Kho, p.ID_DonVi };

            dtgKho.DataSource = q;
            
            gridView1.Columns.ColumnByFieldName("ID_Kho").Visible = false;
            gridView1.Columns.ColumnByFieldName("ID_DonVi").Visible = false;
            
            //focus
            int focusrow = 0;
            if (Sname != "")
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    // if (dtcustomer.Rows[i]["CUST_NAME"].ToString().ToUpper() == Sname.ToUpper())
                    if (gridView1.GetRowCellValue(i, "Kho").ToString().ToUpper() == Sname.ToUpper())
                    {
                        focusrow = i;
                    }
                }
            }         

            gridView1.FocusedRowHandle = focusrow;

        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtKho.EditValue = gridView1.GetFocusedRowCellValue("Kho");
        }


        Boolean fThem = false;
        Boolean fSua = false;
        CKho m_kho = new CKho();

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtKho.Text = "";
            txtKho.Focus();
            fThem = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtKho.Focus();
            fSua = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            //Kiểm tra trong tblTon va tblDPNX
            int ID_Kho = 0;
            ID_Kho = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_Kho"));
            tblKho kho = m_kho.SelectRecordByID(ID_Kho);

            QLVTDataContext dc = new QLVTDataContext();
            var tt_Ton = (from p in dc.tblTons where p.ID_Kho == ID_Kho select p).Count();
            var tt_DPNX = (from p in dc.tblDPNXes where p.ID_Kho == ID_Kho select p).Count();

            if (tt_Ton > 0 || tt_DPNX > 0)
            {
                MessageBox.Show("Kho: " + txtKho.Text + " đang sử dụng, không xóa được.", "Chú ý");
                return;
            }         

            if (MessageBox.Show("Bạn muốn xóa kho: " + txtKho.Text , "Chú ý", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {  
                // Xóa kho
                m_kho.Delete(ID_Kho);
                // Xóa đơn vị
                CDonVi m_donvi = new CDonVi();
                m_donvi.Delete(Convert.ToInt32(kho.ID_DonVi));
                KhoiTaodtgKho("");
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            CDonVi m_DonVi = new CDonVi();
            if (fThem)
            {
                
                tblDonVi donvi = new tblDonVi();
                donvi.DonVi = txtKho.Text;
                donvi.DiaChi = "Nhà máy A41";
                donvi.Nhom = "";
                donvi.Cuoi = true;
                donvi.ID_Parent = 2;// Nhà máy A41
                m_DonVi.Insert(donvi);

                tblKho kho = new tblKho();
                kho.Kho = txtKho.Text;
                kho.ID_DonVi = donvi.ID_DonVi;
                m_kho.Insert(kho);
            }

            if (fSua)
            {                
                int ID_Kho = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_Kho"));
                tblKho kho = m_kho.SelectRecordByID(ID_Kho);

                tblKho kho_Update = new tblKho();
                kho_Update.ID_Kho = kho.ID_Kho;
                kho_Update.Kho = txtKho.Text;
                kho_Update.ID_DonVi = kho.ID_DonVi;                
                m_kho.Update(kho_Update);
                

                //Update DonVi
                CDonVi m_donvi = new CDonVi();
                int ID_DonVi = Convert.ToInt32(kho.ID_DonVi);
                tblDonVi obj_DonVi = m_DonVi.SelectRecordByID(ID_DonVi);

                tblDonVi obj_DVUpdate = new tblDonVi();
                obj_DVUpdate.ID_DonVi = obj_DonVi.ID_DonVi;
                obj_DVUpdate.ID_Parent = obj_DonVi.ID_Parent;
                obj_DVUpdate.Nhom = obj_DonVi.Nhom;
                obj_DVUpdate.Cuoi = obj_DonVi.Cuoi;
                obj_DVUpdate.DiaChi = obj_DonVi.DiaChi;
                obj_DVUpdate.DonVi = txtKho.Text;
                m_DonVi.Update(obj_DVUpdate);
            }

            KhoiTaoGiaoDien(true);
            KhoiTaodtgKho(txtKho.Text);
            fThem = false;
            fSua = false;

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(true);
            fThem = false;
            fSua = false;
            String Kho = Convert.ToString(gridView1.GetFocusedRowCellValue("Kho"));
            KhoiTaodtgKho(Kho);
        }

        public void KhoiTaoGiaoDien(Boolean f)
        {
            btnThem.Enabled = f;
            btnSua.Enabled = f;
            btnXoa.Enabled = f;
            btnGhi.Enabled = !f;
            btnHuy.Enabled = !f;
            txtKho.Properties.ReadOnly = f;
        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            // Khoá dtg khi thêm hoặc sửa
            if (btnThem.Enabled == true)
                (e as DXMouseEventArgs).Handled = false;
            else
                (e as DXMouseEventArgs).Handled = true;
        }

        
       // Dùng để ẩn dấu + trước các row
        //private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        //{
        //    //Hide Master - Detail gridview
        //    GridView view = sender as GridView;

        //    //if (e.Column.VisibleIndex == 0 && view.IsMasterRowEmpty(e.RowHandle))

        //    (e.Cell as GridCellInfo).CellButtonRect = Rectangle.Empty;
        //}
        
    }
}
